package com.jcfk.system.controller;

import cn.hutool.core.util.StrUtil;
import com.jcfk.common.business.log.annotation.RequestLogHeader;
import com.jcfk.common.web.domain.DataPage;
import com.jcfk.common.web.domain.ReqPageParam;
import com.jcfk.common.web.domain.RetPage;
import com.jcfk.common.web.domain.RetResult;
import com.jcfk.system.service.SysWorkbenchInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import javax.validation.Valid;

import com.jcfk.system.domain.query.SysWorkbenchInfoQuery;
import com.jcfk.system.domain.dto.SysWorkbenchInfoDTO;

import java.util.List;

/**
 * 工作台信息
 *
 * @author libinghai
 * @email 35738025@gqq.com
 * @date 2024-01-29
 */
@Api(value = "sysworkbenchinfo", tags = {"工作台信息"})
@RestController
@RequestMapping("sysworkbenchinfo")
public class SysWorkbenchInfoController {
    @Autowired
    private SysWorkbenchInfoService sysWorkbenchInfoService;

    /**
     * 分页列表
     */
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "body", dataType = "ReqPageParam<SysWorkbenchInfoQuery>", name = "requestParam", value = "", required = true)
    })
    @ApiOperation(value = "分页列表", notes = "分页列表", httpMethod = "POST")
    @PostMapping("/queryByPage")
    public RetPage<SysWorkbenchInfoDTO> queryByPage(@RequestBody ReqPageParam<SysWorkbenchInfoQuery> requestParam) {
        DataPage<SysWorkbenchInfoDTO> page = new DataPage<SysWorkbenchInfoDTO>((long) requestParam.getCurrent(),
                (long) requestParam.getSize(),
                requestParam.getSort().getOrderItem(),
            SysWorkbenchInfoDTO.class);
        page = sysWorkbenchInfoService.queryByPage(page, requestParam.getData());

        return new RetPage<>(page);
    }


    /**
     *  工作台信息
     * 根据id 查询详情
     */
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "path", dataType = "string", name = "id", value = "", required = true)
    })
    @ApiOperation(value = "工作台信息 根据id 查询详情", notes = "工作台信息 根据id 查询详情", httpMethod = "GET")
    @GetMapping("/get/{id}")
    public RetResult<SysWorkbenchInfoDTO> get(@PathVariable("id") String id) {
            SysWorkbenchInfoDTO item = sysWorkbenchInfoService.get(id);
        return RetResult.ok(item);
    }

    /**
     * 添加/更新管理工作台信息
     */
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "body", dataType = "SysWorkbenchInfoDTO", name = "sysWorkbenchInfo", value = "", required = true)
    })
    @ApiOperation(value = "添加/更新管理工作台信息", notes = "添加/更新管理工作台信息", httpMethod = "POST")
    @PostMapping("/save")
    @RequestLogHeader(modular = "工作台信息管理", businessType = "保存")
    public RetResult<?> save(@RequestBody @Valid SysWorkbenchInfoDTO sysWorkbenchInfo) {
        // 数据校验
        RetResult ret = sysWorkbenchInfoService.isValid(sysWorkbenchInfo);
        if (!ret.getCode().equals(RetResult.SUCCESS)) {
            return ret;
        }

        if (StrUtil.isEmpty(sysWorkbenchInfo.getId())){
            // 新增
            sysWorkbenchInfoService.insert(sysWorkbenchInfo);
        } else{
            // 更新
            sysWorkbenchInfoService.update(sysWorkbenchInfo);
        }
        return RetResult.ok();

    }

    /**
     *  批量删除工作台信息
     */
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "body", dataType = "SysWorkbenchInfoDTO", name = "sysWorkbenchInfo", value = "", required = true)
    })
    @ApiOperation(value = "批量删除工作台信息", notes = "批量删除工作台信息", httpMethod = "POST")
    @PostMapping("/delete")
    @RequestLogHeader(modular = "工作台信息管理", businessType = "批量删除")
    public RetResult<?> delete(@RequestBody SysWorkbenchInfoDTO sysWorkbenchInfo) {
        return RetResult.ok(sysWorkbenchInfoService.logicDeleteList(sysWorkbenchInfo));
    }

    /**
     * 根据模板创建工作台
     * @param sysWorkbenchInfo
     * @param templateId
     * @return
     */
    @PostMapping("/createByTemplate/{templateId}")
    @RequestLogHeader(modular = "工作台信息管理", businessType = "根据模板创建")
    public RetResult<?> createByTemplate(@RequestBody @Valid SysWorkbenchInfoDTO sysWorkbenchInfo,@PathVariable("templateId") String templateId) {
        // 数据校验
        RetResult ret = sysWorkbenchInfoService.isValid(sysWorkbenchInfo);
        if (!ret.getCode().equals(RetResult.SUCCESS)) {
            return ret;
        }

        // 新增
        sysWorkbenchInfoService.createByTemplate(templateId,sysWorkbenchInfo);

        return RetResult.ok();

    }

    /**
     * 获取公共模板列表
     */
    @GetMapping("/getTemplateList")
    public RetResult<?> getTemplateList() {
        SysWorkbenchInfoQuery query=new SysWorkbenchInfoQuery();
        query.setIsPublic(1);
        List<SysWorkbenchInfoDTO> list=sysWorkbenchInfoService.getList(query);
        return RetResult.ok(list);
    }

    /**
     * 获取个人工作台
     * @param defaultId 默认工作台主键
     * @return
     */
    @GetMapping("/getPersonal/{defaultId}")
    public RetResult<SysWorkbenchInfoDTO> getPersonal(@PathVariable("defaultId") String defaultId){
        SysWorkbenchInfoDTO item=new SysWorkbenchInfoDTO();
        item=sysWorkbenchInfoService.getPersonal(defaultId);
        return RetResult.ok(item);
    }

    /**
     * 新建个人工作台
     * @param defaultId 默认工作台主键
     * @return
     */
    @GetMapping("/createPersonal/{defaultId}")
    public RetResult<SysWorkbenchInfoDTO> createPersonal(@PathVariable("defaultId") String defaultId){
        SysWorkbenchInfoDTO item=new SysWorkbenchInfoDTO();
        item=sysWorkbenchInfoService.createPersonal(defaultId);
        return RetResult.ok(item);
    }
}
